<html>
<head><meta charset="utf-8"><title>Return of &amp;mut does not release borrow within fn · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html">Return of &amp;mut does not release borrow within fn</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218184553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20of%20%26mut%20does%20not%20release%20borrow%20within%20fn/near/218184553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html#218184553">(Nov 28 2020 at 22:39)</a>:</h4>
<p>Is there any reason for a <code>return x</code> (where <code>x: &amp;mut _</code>) to not release the borrows that <code>x</code> implied with respect to code that follows the return? For instance: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=03064197395bf9f824e5cfe10da80dfd">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=03064197395bf9f824e5cfe10da80dfd</a> fails right now, because returning an element from the vector retains the borrow on the vector itself to the end of the function (even though we are returning here...?)</p>



<a name="218184604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20of%20%26mut%20does%20not%20release%20borrow%20within%20fn/near/218184604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html#218184604">(Nov 28 2020 at 22:40)</a>:</h4>
<p>It seems like not all kinds of borrows have this kind of behaviour… this works for example <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=721b9924cef70949d7ec5617b819a845">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=721b9924cef70949d7ec5617b819a845</a></p>



<a name="218185701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20of%20%26mut%20does%20not%20release%20borrow%20within%20fn/near/218185701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html#218185701">(Nov 28 2020 at 23:14)</a>:</h4>
<p>I'd expect this to be <a href="https://doc.rust-lang.org/nomicon/lifetime-mismatch.html#improperly-reduced-borrows">https://doc.rust-lang.org/nomicon/lifetime-mismatch.html#improperly-reduced-borrows</a> or something like it</p>



<a name="218208696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20of%20%26mut%20does%20not%20release%20borrow%20within%20fn/near/218208696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html#218208696">(Nov 29 2020 at 11:30)</a>:</h4>
<p>NLL problem case 3 <a href="https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions">https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions</a> and similar issues (like <a href="https://github.com/rust-lang/rust/issues/47680">#47680</a>), involving more complex control flow, were postponed from the NLL implementation (and to be solved in the future via polonius, which should already accept your example, or something else)</p>



<a name="218211632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20of%20%26mut%20does%20not%20release%20borrow%20within%20fn/near/218211632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20of.20.26mut.20does.20not.20release.20borrow.20within.20fn.html#218211632">(Nov 29 2020 at 12:54)</a>:</h4>
<p><a href="https://stackoverflow.com/q/38023871/155423">Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it's in?</a></p>
<p><a href="https://stackoverflow.com/q/50519147/155423">Double mutable borrow error in a loop happens even with NLL on</a></p>
<p><a href="https://stackoverflow.com/q/64902078/155423">Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>